/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getOrderDetailsList,
  closeOrder,
  addExportOrderTask,
  getOrderExportList,
  dispatchOrder,
  getBrands,
} from "@/services/api";
import { message } from "antd";
import { isArray } from "lodash";
import { isLegalData, setLStorage } from "@/utils/utils";

export default {
  namespace: "orderDetails",
  state: {
    orderDetailList: {},
    orderExportList: {},
  },

  effects: {
    *addExportOrderTask({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(addExportOrderTask, payload);
      const { code, data, msg } = response;
      if (code === 0) {
        __dva_resolve(data);
        message.success("添加导出任务成功,请在导出列表中查看/导出");
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    *getOrderExportList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getOrderExportList, payload);
      const {
        code,
        msg,
        data: { list, total, pageNum, page },
      } = response;
      if (code === 0) {
        const orderExportList = {
          list: isArray(list) ? list : [],
          pagination: { total, current: page, pageSize: pageNum },
        };
        __dva_resolve(orderExportList);
        yield put({
          type: "save",
          payload: {
            orderExportList,
          },
        });
        __dva_resolve(orderExportList);
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    *closeOrder({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(closeOrder, payload);
      const { code, msg } = response;
      if (code === 0) {
        __dva_resolve();
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    *getOrderDetailsList({ payload, __dva_resolve }, { call, put, select }) {
      const { shop_id } = yield select(
        ({ user: { currentUser: { user_info } = {} } = {} }) => user_info
      );
      const response = yield call(getOrderDetailsList, payload);
      if (!response) return;
      const { pageSize = 10, page = 1 } = payload;
      const {
        code,
        data: { list = [], pageNum = 0 },
        msg,
      } = response;

      (list || []).forEach((i, index) => {
        i.disabled = !!i.waybill_no || i.statusText == "已取消";
        i.num = index + 1 + pageSize * (page - 1);
      });

      if (code == 0 && response.data) {
        setLStorage(`${shop_id}orderDetailPageSize`, pageSize);
        const orderDetailList = {
          list: isArray(list) ? list : [],
          pagination: {
            total: pageNum * 1,
            current: page * 1,
            pageSize,
            showQuickJumper: false,
            pageSizeOptions: ["10", "20", "30", "40", "50"],
          },
        };
        __dva_resolve(orderDetailList);
        yield put({
          type: "save",
          payload: {
            orderDetailList,
          },
        });
      } else {
        message.error(msg);
      }
    },
    *dispatchOrder({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(dispatchOrder, payload);
      const { code, msg } = response;
      if (code === 0) {
        __dva_resolve();
        message.success("分配成功！");
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    *getBrands({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getBrands, payload);
      const { code, data, msg } = response;
      if (code === 0) {
        __dva_resolve(isLegalData(data));
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
